package com.pdd.controller;

import com.pdd.beans.Order;
import com.pdd.dto.DTO;
import com.pdd.service.OrderService;
import com.pdd.vo.order.OrderAddVO;
import com.pdd.vo.order.OrderUplVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 *
 * 订单控制器
 */
@Api(description = "订单接口",tags = {"订单接口"})
@CrossOrigin(origins = { "*" })
@Controller
public class OrderController {

    @Autowired
    @Setter
    private OrderService orderService;

    @ApiOperation(value = "添加订单信息")
    @RequestMapping(value = "/order", method = RequestMethod.POST)
    @ResponseBody
    public DTO addOrder(OrderAddVO vo){
        // 1. 封装参数
        Order order = new Order();
        order.setO_id(vo.getO_id());
        order.setU_id(vo.getU_id());
        order.setAddress(vo.getAddress());
        order.setName(vo.getName());
        order.setTel(vo.getTel());
        order.setMoney(vo.getMoney());

        // 2. 执行新增
        boolean result = orderService.addOrder(order);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }
    @ApiOperation(value = "删除订单信息")
    @RequestMapping(value = "/order/{o_id}", method = RequestMethod.DELETE)
    public DTO delOrder(@PathVariable(value = "o_id") String o_id){

        boolean result = orderService.delOrder(o_id);
        if (result){
            return DTO.success();
        }else {
            return DTO.error();
        }
    }

    @ApiOperation(value = "更新订单信息")
    @RequestMapping(value = "/order/{o_id}", method = RequestMethod.PUT)
    @ResponseBody
    public DTO uplOrder(OrderUplVO vo,@PathVariable(value = "o_id") String o_id){
        // 1. 封装参数
        Order order = new Order();
        order.setAddress(vo.getAddress());
        order.setName(vo.getName());
        order.setTel(vo.getTel());
        // 2. 执行更新
        boolean result = orderService.uplOrder(order);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }

    @ApiOperation(value = "查询订单信息")
    @RequestMapping(value = "/order", method = RequestMethod.GET)
    @ResponseBody
    public DTO getOrder(){
        // 1. 执行查询操作
        List<Order> list = orderService.getOrder();

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
    @ApiOperation(value = "根据ID查询订单信息")
    @RequestMapping(value = "/order/{o_id}", method = RequestMethod.GET)
    @ResponseBody
    public DTO getById(@PathVariable(value = "o_id") Integer o_id){
        // 1. 执行查询操作
        Order list = orderService.getById(o_id);

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
}
